﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BAF.Entity;
using System.Data;

namespace Card.Common
{
    public class LookUpCache
    {
        private static Dictionary<string, LookUpType> _LookUpType;
        private static Dictionary<string, List<LookupValue>> _LookUp;

        static LookUpCache()
        {
            Refresh();
        }

        public static List<LookupValue> GetLookupValues(string lookUpType)
        {
            try
            {
                _LookUp[lookUpType].Sort(new Comparison<LookupValue>(delegate(LookupValue v, LookupValue v1) { return v.Sort > v1.Sort ? 1 : (v.Sort == v1.Sort ? 0 : -1); }));
                return _LookUp[lookUpType];
            }
            catch (Exception ex)
            {
                BAF.Log.Logger.Error(string.Format("LookUpType:{0}\n Exception:{1}", lookUpType,ex.ToString()));
                throw new Exception(string.Format("GetLookupValues LookUpType:{0}", lookUpType), ex);
            }
        }

        public static LookupValue GetLookupValue(string lookUpType, string lookUpCode)
        {
            try
            {
                return _LookUp[lookUpType].Single<LookupValue>(e => { return string.Compare(e.LookupCode, lookUpCode,true) == 0; });
            }
            catch (Exception ex)
            {
                BAF.Log.Logger.Error(string.Format("LookUpType:{0}\nlookUpCode:{1}\nException:{2}", lookUpType, lookUpCode,ex.ToString()));
                throw new Exception(string.Format("GetLookupValue LookUpType:{0}\nlookUpCode:{1}", lookUpType, lookUpCode), ex);
            }
        }

        public static void Refresh()
        {
            DataTable lookupTypes = BAF.BLL.LookupTypeManager.GetList(string.Empty, null).Tables[0];
            DataTable lookupValues = BAF.BLL.LookupValueManager.GetList(string.Empty, null).Tables[0];

            Dictionary<string, LookUpType> lookUpType = new Dictionary<string, LookUpType>();
            Dictionary<string, List<LookupValue>> lookUpValue = new Dictionary<string, List<LookupValue>>();

            foreach (DataRow drSource in lookupTypes.Rows)
            {
                lookUpType.Add(drSource["LOOKUP_TYPE"].ToString(), new LookUpType()
                {
                    LookupType = drSource["LOOKUP_TYPE"].ToString(),
                    Description = drSource["DESCRIPTION"].ToString(),
                    Meaning = drSource["MEANING"].ToString()
                });

                var lstValues = (from row in lookupValues.AsEnumerable()
                                 where string.Compare(row["LOOKUP_TYPE"].ToString(), drSource["LOOKUP_TYPE"].ToString()) == 0
                                 select new LookupValue()
                                 {
                                     LookupType = row["LOOKUP_TYPE"].ToString(),
                                     LookupCode = row["LOOKUP_CODE"].ToString(),
                                     Value = row["VALUE"].ToString(),
                                     TextValue = row["TEXT_VALUE"].ToString(),
                                     Description = row["DESCRIPTION"].ToString(),
                                     Sort = Card.Utility.ValidationHelper.IsNumber(row["SORT"].ToString()) ? int.Parse(row["SORT"].ToString()) : 0,
                                     Attribute1 = row["ATTRIBUTE1"].ToString(),
                                     Attribute2 = row["ATTRIBUTE2"].ToString(),
                                     Attribute3 = row["ATTRIBUTE3"].ToString(),
                                     Attribute4 = row["ATTRIBUTE4"].ToString(),
                                     Attribute5 = row["ATTRIBUTE5"].ToString()
                                 }).ToList();

                lstValues.Sort(new Comparison<LookupValue>(delegate(LookupValue l1, LookupValue l2) { return l1.Sort > l2.Sort ? 1 : (l1.Sort == l2.Sort ? 0 : -1); }));
                lookUpValue.Add(drSource["LOOKUP_TYPE"].ToString(), lstValues);
            }

            _LookUpType = lookUpType;
            _LookUp = lookUpValue;
        }
    }
}
